package com.healthcloud.service.index.impl;

import com.google.common.collect.Lists;
import com.healthcloud.dao.index.CountDao;
import com.healthcloud.dto.res.CountInstrumentMonthDto;
import com.healthcloud.service.index.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CountServiceImpl implements CountService {

    @Autowired
    private CountDao countDao;

    @Override
    public Integer patientNum(String ids, String type) {
        return countDao.patientNum(ids, type);
    }

    /**
     * 2018年至当年仪器总数
     */
    @Override
    public Integer instrumentNumber(String ids, String type) {
        if ("0".equals(type)) {
            return countDao.instrumentNumber1(ids, type) + countDao.instrumentNumber2(ids, type);
        } else if ("1".equals(type) ) {
            return countDao.instrumentNumber1(ids, type);
        } else if ("2".equals(type)) {
            return countDao.instrumentNumber2(ids, type);
        }
            return 0;
    }

    @Override
    public Integer patientNewNum(String ids, String type) {
        return countDao.patientNewNum(ids, type);
    }

    /**近30天仪器数量*/
    @Override
    public Integer instrumentNewNum(String ids, String type) {
        return countDao.instrumentNewNum(ids,type);
    }

    @Override
    public Integer instrumentUseNum(String ids, String type, String dateType) {
        return countDao.instrumentUseNum(ids, type, dateType);
    }

    @Override
    public List<Map> tenUseMsg(String ids, String type) {
        return countDao.tenUseMsg(ids, type);
    }

    @Override
    public Integer instrumentActiveNum(String ids, String type, String dateType) {
        return countDao.instrumentActiveNum(ids, type, dateType);
    }

    @Override
    public List<Map> instrumentUseDate(String ids, String type, String dateType) {
        return countDao.instrumentUseDate(ids, type, dateType);
    }

    @Override
    public List<Map> instrumentActiveDate(String ids, String type, String dateType) {
        return countDao.instrumentActiveDate(ids, type, dateType);
    }

    @Override
    public List<Map> patientMonth(String ids, String type, String year) {
        return countDao.patientMonth(ids, type, year);
    }

    @Override
    public List<Map> instrumentMonth(String ids, String type, String year) {
        return countDao.instrumentMonth(ids, type, year);
    }

    /**
     * 按月统计设备绑定数（区分慢病后台绑定 和 积分商城绑定）
     *
     * @param ids
     * @param type
     * @param year
     * @return
     */
    @Override
    public List<CountInstrumentMonthDto> countInstrumentMonth(String ids, String type, String year) {
        List<CountInstrumentMonthDto> msList = countDao.countInstrumentMonthFromMS(ids, type, year); // 统计慢病后台绑定的设备绑定数
        List<CountInstrumentMonthDto> inteList = countDao.countInstrumentMonthFromIntegral(ids, type, year); // 统计积分商城绑定的设备绑定数
        List<CountInstrumentMonthDto> resultList = Lists.newArrayList();
        for (int i = 1; i <= 12; i++) {
            int countFromMS = 0; // 慢病后台绑定设备合计数
            int countFromIntegral = 0; // 积分商城绑定设备合计数
            for (CountInstrumentMonthDto msDto : msList) {
                if (msDto.getCurrMonth().equals(String.valueOf(i))) {
                    countFromMS = msDto.getCountFromMS();
                }
            }
            for (CountInstrumentMonthDto inteDto : inteList) {
                if (inteDto.getCurrMonth().equals(String.valueOf(i))) {
                    countFromIntegral = inteDto.getCountFromIntegral();
                }
            }
            resultList.add(CountInstrumentMonthDto.builder()
                    .currMonth(String.valueOf(i))
                    .countFromMS(countFromMS)
                    .countFromIntegral(countFromIntegral)
                    .build());
        }
        // 计算「每月绑定设备合计数」countAll = countFromMS + countFromIntegral
        for (CountInstrumentMonthDto dto : resultList) {
            dto.setCountAll(dto.getCountFromIntegral() + dto.getCountFromMS());
        }
        return resultList;
    }

    /**
     * 随访月度统计
     */
    @Override
    public List<Map> accompanyMonth(String ids, String type, String year) {
        return countDao.accompanyMonth(ids, type, year);
    }

    @Override
    public List<Map> newPatientMonth(String ids, String type, String year) {
        return countDao.newPatientMonth(ids, type, year);
    }

}